package com.lepu.app.fun.my.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.utils.Setting;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.eyzhs.app.R;
import com.lepu.app.fun.knowledge.bean.Article;

public class AdapterSearchArticle extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	@SuppressWarnings("unused")
	private Context mContext = null;
	private ArrayList<Article> mArrayList = null;

	public AdapterSearchArticle(Context context,
			ArrayList<Article> arrayList) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(
					R.layout.my_search_result_listitem, null);

			holder = new ViewHolder();

			ImageView iconImageView = (ImageView) convertView
					.findViewById(R.id.iconImageView);
			TextView titleTextView = (TextView) convertView
					.findViewById(R.id.titleTextView);
			TextView subTitleTextView = (TextView) convertView
					.findViewById(R.id.subTitleTextView);

			holder.iconImageView = iconImageView;
			holder.titleTextView = titleTextView;
			holder.subTitleTextView = subTitleTextView;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final Article item = mArrayList.get(position);

//		holder.iconImageView.setImageBitmap(ImageUtilBase.getBitmapByResId(
//				mContext, R.drawable.default_icon_small));
//		if (!TextUtils.isEmpty(item.Cover)) {
//			String fileName = EncodeMD5.getMd5(item.Cover);
//			String filePath = SdLocal.getCacheImagePath(mContext, fileName);
//			String imageUrl = Setting.getImageUrl(item.Cover);
//
//			BitmapManager.getInstance().loadBitmap(imageUrl, filePath,
//					holder.iconImageView, true);
//		}
		String imageUrl = Setting.getImageUrl(item.Cover);
		UniversalImageLoadTool.disPlay(imageUrl, holder.iconImageView, R.drawable.default_icon_small);

		holder.titleTextView.setText(item.ArticleTitle);
		holder.subTitleTextView.setText(item.ArticleDescription);
		
		return convertView;
	}

	private static class ViewHolder {

		ImageView iconImageView;
		TextView titleTextView;
		TextView subTitleTextView;
	}
}
